package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    /**
     * 需要的数据
     *      months: [2018.10,.....,2019.9]
     *      memberCount: [100,.....,1000]
     * @return
     */
    @Override
    public Map<String, Object> getMemberReport() {

        //获取最近的12个月
        List<String> months = new ArrayList<>();
        List<Long> memberCount = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -11);

        for (int i = 0; i < 12; i++) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String str = simpleDateFormat.format(calendar.getTime());
            calendar.add(Calendar.MONTH, 1);
            months.add(str);
        }
        //memberCount:每个会员数量
        for (String date : months) {
            // 2018-10-31
            date += "-31";
            long count = memberDao.findByBeforeDate(date);
            memberCount.add(count);
        }

        //组合数据
        Map<String,Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);

        return map;
    }
    @Override
    public Result findMemberByDate(String firstDate, String endDate) {
        try {
            //前段页面所需要的数据格式
            Map<String,Object> map = new HashMap<>();
            List<String> months = new ArrayList<>();
            List<Long> memberCount= new ArrayList<>();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            //如果起始时间和结束时间都不为空
            if (!("null".equals(endDate)&&!"null".equals(firstDate))&&firstDate!=null&&endDate!=null){
                Date first = simpleDateFormat.parse(firstDate);
                //转换成毫秒值进行比较
                long firstTime = first.getTime();
                Date end = simpleDateFormat.parse(endDate);
                long endTime = end.getTime();
                long thisTime = new Date().getTime();
                
                //如果起始时间和结束时间任意一个大于当前时间 则给出提示 并执行默认查询
                if (endTime>thisTime||firstTime>thisTime){
                    //调用getMemberReport方法 获取所有会员
                    Map<String, Object> memberReport = getMemberReport();

                    return new Result(false,"结束日期和开始日期必须小于等于今天,默认查询全部会员",memberReport);
                }
                //如果结束时间小于起始时间  则给出提示 并执行默认查询
                 if (endTime<firstTime){
                    //调用调用getMemberReport方法 获取所有会员
                    Map<String, Object> memberReport = getMemberReport();
                    //System.out.println("查询错误");
                    return new Result(false,"起始日期必须小于结束日期,默认查询全部会员",memberReport);
                }

               //正常的时间查询
                List<String> month = getMonth(firstDate, endDate);
                for (String s : month) {
                    long count=memberDao.findMember4Date(s);
                    memberCount.add(count);
                    //months.add(s);
                }
                List<String> monthList = getMonths(firstDate, endDate);
                for (String s : monthList) {
                    months.add(s);
                }
                map.put("months",months);
                map.put("memberCount",memberCount);
                return new Result(true,"根据条件查询成功",map);

            } else{
                //如果起始时间为空 结束时间不大于当前时间 那么查询从结束时间之前的所有会员
                if("null".equals(firstDate)||firstDate==null&&simpleDateFormat.parse(endDate).getTime()<new Date().getTime()){

                    List<String> month = getMonth(firstDate, endDate);
                    for (String s : month) {
                        long count=memberDao.findMemberByEndDate(s);
                        memberCount.add(count);
                    }
                    List<String> monthList = getMonths(firstDate, endDate);
                    for (String s : monthList) {
                        months.add(s);
                    }
                    map.put("months",months);
                    map.put("memberCount",memberCount);
                    return new Result(true,"结束日期前所有会员",map);
                }
                //如果结束时间为空 起始时间不大于当前时间 那么查询从起始时间到当前时间的所有会员
                if ("null".equals(endDate)||endDate==null&&simpleDateFormat.parse(firstDate).getTime()<new Date().getTime()){
                    String thisDate = simpleDateFormat.format(new Date());
                    List<String> month = getMonth(firstDate, thisDate);
                    for (String s : month) {
                        long count =memberDao.findMemberByFirstDate(s);
                        memberCount.add(count);

                    }
                    List<String> monthList = getMonths(firstDate, thisDate);
                    for (String s : monthList) {
                        months.add(s);
                    }
                    map.put("months",months);
                    map.put("memberCount",memberCount);
                    return new Result(true,"起始日期到当前日的所有会员",map);
                }
            }
            //不知道会遇到什么错误 抄底用的
            return new Result(false,"未知错误,请联系管理员");
        } catch (Exception e) {
            return new Result(false,"请选择正确日期");
        }
    }


    //自己编的工具 判断是否跨年 遍历月份 拼接时间
    private List<String> getMonth(String firstDate,String endDate){
        //如果起始时间为空
        //为初始时间赋值
        if (firstDate == null) {
            String[] endSplit = endDate.split("-");
            String endYear = endSplit[0];
            int endYearInt = Integer.parseInt(endYear);
            firstDate=String.valueOf(endYearInt-1)+"-"+endSplit[1]+"-"+endSplit[2];
        }
        //2019-01-01
        String[] firstSplit = firstDate.split("-");
        String firstMonth = firstSplit[1];
        int firstMonthInt = Integer.parseInt(firstMonth);
        int firstYearInt=Integer.parseInt(firstSplit[0]);
        //2019-10-10
        String[] endSplit = endDate.split("-");
        String endMonth = endSplit[1];
        int endMonthInt = Integer.parseInt(endMonth);
        int endYearInt=Integer.parseInt(endSplit[0]);
        List<String> list = new ArrayList<>();
        //结束年大于初始年
        if (endYearInt>firstYearInt){
            String date;
            //如果大于一年 月份从起始月份到12月 拼接初始年份
            for (int i = firstMonthInt; i <=12 ; i++) {
                //如果大于10月 则不需要补0
                if(i>=10){
                    date=firstSplit[0]+"-"+String.valueOf(i)+"-31";
                }else{
                    //如果小于10月 则需要在月份前补一个0
                    date=firstSplit[0]+"-0"+String.valueOf(i)+"-31";
                }
                list.add(date);
            }
            //从一月到结束月份  拼接结束年份
            for (int j = 1; j <=endMonthInt ; j++) {
                if(j>=10){
                    date=endSplit[0]+"-"+String.valueOf(j)+"-31";
                }else{
                    date=endSplit[0]+"-0"+String.valueOf(j)+"-31";
                }
                list.add(date);
            }
        }else{
            //如果年份相等 只拼接月份
            for (int i = firstMonthInt; i <=endMonthInt; i++) {
                String date;
                if(i>=10){
                    date=firstSplit[0]+"-"+String.valueOf(i)+"-31";
                }else{
                    date=firstSplit[0]+"-0"+String.valueOf(i)+"-31";
                }
                list.add(date);
            }
        }
        return list;
    }

    private List<String> getMonths(String firstDate,String endDate){
        //如果起始时间为空
        //为初始时间赋值
        if (firstDate == null) {
            String[] endSplit = endDate.split("-");
            String endYear = endSplit[0];
            int endYearInt = Integer.parseInt(endYear);
            firstDate=String.valueOf(endYearInt-1)+"-"+endSplit[1];
        }
        //2019-01-01
        String[] firstSplit = firstDate.split("-");
        String firstMonth = firstSplit[1];
        int firstMonthInt = Integer.parseInt(firstMonth);
        int firstYearInt=Integer.parseInt(firstSplit[0]);
        //2019-10-10
        String[] endSplit = endDate.split("-");
        String endMonth = endSplit[1];
        int endMonthInt = Integer.parseInt(endMonth);
        int endYearInt=Integer.parseInt(endSplit[0]);
        List<String> monthList = new ArrayList<>();
        //结束日期大于初始时间
        if (endYearInt>firstYearInt){
            String date;
            //如果大于一年 月份从起始月份到12月 拼接初始年份
            for (int i = firstMonthInt; i <=12 ; i++) {
                //如果大于10月 则不需要补0
                if(i>=10){
                    date=firstSplit[0]+"-"+String.valueOf(i);
                }else{
                    //如果小于10月 则需要在月份前补一个0
                    date=firstSplit[0]+"-0"+String.valueOf(i);
                }
                monthList.add(date);
            }
            //从一月到结束月份  拼接结束年份
            for (int j = 1; j <=endMonthInt ; j++) {
                if(j>=10){
                    date=endSplit[0]+"-"+String.valueOf(j);
                }else{
                    date=endSplit[0]+"-0"+String.valueOf(j);
                }
                monthList.add(date);
            }
        }else{
            //如果年份相等 只拼接月份
            for (int i = firstMonthInt; i <=endMonthInt; i++) {
                String date;
                if(i>=10){
                    date=firstSplit[0]+"-"+String.valueOf(i);
                }else{
                    date=firstSplit[0]+"-0"+String.valueOf(i);
                }
                monthList.add(date);
            }
        }
        return monthList;
    }

    /**
     * 统计会员男女数量
     */
    @Override
    public List<Map<String, Object>> userSexCount() {
        return memberDao.getSexCount();
    }

    /**
     * 统计会员年龄段分布
     */
    @Override
    public List<Map<String, Object>> userAgeCount() {
        return memberDao.getAgeCount();
    }
}
